package common.peer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;


public class PeerDescriptor implements Comparable<PeerDescriptor>, Serializable {
	private static final long serialVersionUID = 1906679375438244117L;
	private final PeerAddress peerAddress;
	private int price;
	private float cost;
	private int age;
	private int sp; //Shortest path to the media source

//-------------------------------------------------------------------
	public PeerDescriptor(PeerAddress peerAddress) 
	{
		//this.peerInfo = copyPI(peerInfo);
		this.peerAddress = peerAddress;
		this.age = 0;
		this.price = -1;
		if(peerAddress.isSource())
		{
			this.cost = 0;
			this.sp = 0;
		}
		else
		{
			this.cost = 9999;
			this.sp = 9999;
		}
	}
	
//-------Copy Constructor-------------------------------------------
	public PeerDescriptor copyPD()
	{
		PeerDescriptor peerDescriptor = new PeerDescriptor(getPeerAddress());
		peerDescriptor.setCost(getCost());
		peerDescriptor.setAge(getAge());
		peerDescriptor.setPrice(getPrice());
		peerDescriptor.setSP(getSP());
		return peerDescriptor;
	}
	
//-------------------------------------------------------------------
	public void setSP(int sp)
	{
		this.sp = sp;
	}
	
//-------------------------------------------------------------------
	public int getSP()
	{
		return sp;
	}
	
//-------------------------------------------------------------------
	public int incrementAndGetAge() {
		age++;
		return age;
	}
	
//-------------------------------------------------------------------
	public void setAge(int age)
	{
		this.age = age;
	}

//-------------------------------------------------------------------
	public int getAge() {
		return age;
	}

//-------------------------------------------------------------------
	public PeerAddress getPeerAddress() {
		return peerAddress;
	}
	
//-------------------------------------------------------------------
	public float getCost() {
		return cost;
	}
	
//-------------------------------------------------------------------
	public void setCost(float cost) {
		this.cost = cost;
	}

//-------------------------------------------------------------------
	public int getPrice() {
		return price;
	}
	
//-------------------------------------------------------------------
	public void setPrice(int price) {
		this.price = price;
	}

//-------------------------------------------------------------------
	public int getMoney() {
		return peerAddress.getBandwidth();
	}
	
//-------------------------------------------------------------------
	
	@Override
	public int compareTo(PeerDescriptor that) {
		if (this.age > that.age)
			return 1;
		if (this.age < that.age)
			return -1;
		return 0;
	}

//-------------------------------------------------------------------
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((peerAddress == null) ? 0 : peerAddress.hashCode());
		return result;
	}

//-------------------------------------------------------------------
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PeerDescriptor other = (PeerDescriptor) obj;
		if (peerAddress == null) {
			if (other.peerAddress != null)
				return false;
		} else if (!peerAddress.equals(other.peerAddress))
			return false;
		return true;
	}

//-------------------------------------------------------------------
	@Override
	public String toString() {
		return peerAddress + "";
	}
	
}
